package com.d404.server;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import manager.DefaultSessionManager;

/**
 * Servlet implementation class GetOnlineStatus
 */
public class GetOnlineStatus extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetOnlineStatus() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data,Upload.class);
		
		Download download = new Download();
		
		for(String account:upload.getUsers())
		{
			if (DefaultSessionManager.share().containsCIMSession(account)) {
				download.users.add(account+"1");
			}
			else
			{
				download.users.add(account+"0");
			}
		}
		
		System.out.println(JSON.toJSONString(download));
		response.getWriter().append(JSON.toJSONString(download));
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	 // 获取的数据
    public static class Download
    {
        ArrayList<String> users = new ArrayList<String>();

        public ArrayList<String> getUsers() {
            return users;
        }

        public void setUsers(ArrayList<String> users) {
            this.users = users;
        }
    }

    // 上传的数据
    public static class Upload
    {
        ArrayList<String> users = new ArrayList<String>();

        public ArrayList<String> getUsers() {
            return users;
        }

        public void setUsers(ArrayList<String> users) {
            this.users = users;
        }
    }

}
